package com.uusee.crawler.util;

import org.apache.commons.lang.StringUtils;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

import com.uusee.util.chinese.CnUtil;

public class PageProcessorUtils {
	public static String getValue(String regex, String text,int group) {
		String value = "";

		try {
			PatternCompiler compiler = new Perl5Compiler();
			Pattern pattern = compiler.compile(regex, Perl5Compiler.CASE_INSENSITIVE_MASK);
			PatternMatcher matcher = new Perl5Matcher();
			PatternMatcherInput input = new PatternMatcherInput(text);
			if (matcher.contains(input, pattern)) {
				MatchResult m = matcher.getMatch();
				value = m.group(group);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return value;
	}
	public static boolean isExistFlag(String rule,String flag) {
		boolean exist = true;
		if (rule.indexOf(flag) >= 0) {
			exist = true;
		} else {
			exist = false;
		}
		return exist;
	}
	
     public static String  unicodeToChinese(String str) {
    	 StringBuffer sb = new StringBuffer();
         for ( char c : str.toCharArray() ) {
        	 sb.append(c);
         }
         return sb.toString();
     } 
     
	public static int getLength(String sLen) {
		int length = 0;
		if (StringUtils.isEmpty(sLen)) {
			return length;
		}
		sLen = sLen.replaceAll("'", ":");
		sLen = sLen.replaceAll("&#39;", ":");
		sLen = sLen.replaceAll("&#34;", "");

		String[] lenArray = sLen.split(":");
		if (lenArray.length == 3) {
			length = Integer.valueOf(lenArray[0]) * 60 * 60 + Integer.valueOf(lenArray[1]) * 60
					+ Integer.valueOf(lenArray[2]);
		} else if (lenArray.length == 2) {
			length = Integer.valueOf(lenArray[0]) * 60 + Integer.valueOf(lenArray[1]);
		} else {
			length = Integer.valueOf(lenArray[0]);
		}

		return length;
	}
	
	public static String replaceFlag(String rule, String flag, String value) {
		String result = rule;
		try {
			if (StringUtils.isNotEmpty(rule)) {
				if (rule.indexOf(flag)>=0) {
					result = rule.replaceAll(flag, value);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			result = rule;
		}
		return result;
	}

	public static String getValue(String regex, String text) {
		String value = "";

		try {
			PatternCompiler compiler = new Perl5Compiler();
			Pattern pattern = compiler.compile(regex, Perl5Compiler.CASE_INSENSITIVE_MASK);
			PatternMatcher matcher = new Perl5Matcher();
			PatternMatcherInput input = new PatternMatcherInput(text);
			if (matcher.contains(input, pattern)) {
				MatchResult m = matcher.getMatch();
				value = m.group(1);
			}
		} catch (Exception e) {
			//e.printStackTrace();
		}

		return value;
	}
	
	public static boolean isMatch(String regex, String text) {
		boolean match = true;

		try {
			PatternCompiler compiler = new Perl5Compiler();
			Pattern pattern = compiler.compile(regex, Perl5Compiler.CASE_INSENSITIVE_MASK);
			PatternMatcher matcher = new Perl5Matcher();
			PatternMatcherInput input = new PatternMatcherInput(text);
			if (matcher.contains(input, pattern)) {
				match = true;
			}else {
				match = false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			match = false;
		}

		return match;
	}
	
	public static String getFirstLetter(String name) {
		String firstLetter = "";
		try {
			firstLetter = CnUtil.getFirstLetter(name).toUpperCase();
		} catch (Exception e) {

		}
		return firstLetter;
	}
	
	public static String getValues(String valuesReg, String values) {
		try {
			if (isMatch(valuesReg, values)) {
				values = values.toLowerCase();
				StringBuffer valuesSb = new StringBuffer();
				PatternCompiler compiler = new Perl5Compiler();
				Pattern pattern = compiler.compile(valuesReg, Perl5Compiler.CASE_INSENSITIVE_MASK);
				PatternMatcher matcher = new Perl5Matcher();
				PatternMatcherInput input = new PatternMatcherInput(values);
				while (matcher.contains(input, pattern)) {
					MatchResult m = matcher.getMatch();
					String v = m.group(1);
					if (StringUtils.isNotEmpty(v) && !v.startsWith("更多")) {
						v = v.replaceAll("\\t", "");
						v = v.replaceAll(",", ".");
						v = v.replaceAll("<[^<]*?>", "");
						valuesSb.append(v);
						valuesSb.append(",");
					}
				}
				String result = valuesSb.toString();
				if (result.length() > 1) {
					result = result.substring(0, result.length() - 1);
				}
				return result;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";

	}
	
	public static void main(String[] args) {
		System.out.println ( unicodeToChinese("AASAS中国") );
		System.out.println ( unicodeToChinese("\u674e\u5c0f\u9f99") );
	}
}
